package com.example.prom.ok.y2022.excel;

import com.example.prom.listener.ExcelListener;
import com.example.prom.ok.y2022.enums.TemplateModuleEnum;
import com.example.prom.ok.y2022.model.DataBaseReader;
import com.example.prom.ok.y2022.model.DataBaseWriter;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.alibaba.excel.EasyExcelFactory.read;
import static com.alibaba.excel.EasyExcelFactory.write;

public class TemplateStatisticsExcelHandler {

//    public static void writeExcel(){
//        List<DataBaseModel> userModels = getUserModels();
//        String fileName = "D:\\dangwen1\\Desktop\\excel-test.xlsx";
//        write(fileName, UserModel.class).sheet().head(UserModel.class)
//                .registerWriteHandler(new CustomColumnWidthStrategy())
//                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(userModels);
//    }

    public static void readExcel(){
        List<DataBaseReader> totalTemplateList = readExcel1();
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\enTemplates.xlsx";
        ExcelListener<DataBaseReader> excelListener = new ExcelListener<>();
        read(fileName, DataBaseReader.class, excelListener).sheet().doRead();
        List<DataBaseReader> enTemplateList = excelListener.getDataList();
        List<DataBaseWriter> writerList = new ArrayList<>();
        for (DataBaseReader dataBaseReader : enTemplateList) {
            DataBaseWriter dataBaseWriter = new DataBaseWriter();
            dataBaseWriter.setTemplate(dataBaseReader.getTemplate());
            dataBaseWriter.setBusinessLine(dataBaseReader.getBusinessLine());
            dataBaseWriter.setContent(dataBaseReader.getContent());
            dataBaseWriter.setTitle(dataBaseReader.getTitle());
            dataBaseWriter.setTemplateType(dataBaseReader.getTemplateType());
            String prefix = "smspush_";
            String module = "template";
            String templateType = dataBaseReader.getTemplateType();
            int nodeId = Integer.parseInt(dataBaseReader.getNodeId());
            if (templateType.equals("邮件验证码") || templateType.equals("邮件通知") || templateType.equals("邮件营销")) {
                prefix = "email_";
                module = TemplateModuleEnum.getModuleNameByNodeId(nodeId).equals("") ? TemplateModuleEnum.getModuleNameByNodeId(0) : TemplateModuleEnum.getModuleNameByNodeId(nodeId);
            }
            dataBaseWriter.setLocale(dataBaseReader.getLocale());
            dataBaseWriter.setLokaliseTitleKey((prefix + module + "_" + dataBaseReader.getTemplate() + "_title").toLowerCase());
            dataBaseWriter.setLokaliseContentKey((prefix + module + "_" + dataBaseReader.getTemplate() + "_content").toLowerCase());
            writerList.add(dataBaseWriter);
        }

        List<String> enTemplateNameList = enTemplateList.stream().map(DataBaseReader::getTemplate).collect(Collectors.toList());
        for (DataBaseReader dataBaseReader : totalTemplateList) {
            if (!enTemplateNameList.contains(dataBaseReader.getTemplate())){
                // logic
                DataBaseWriter dataBaseWriter = new DataBaseWriter();
                dataBaseWriter.setTemplate(dataBaseReader.getTemplate());
                dataBaseWriter.setBusinessLine(dataBaseReader.getBusinessLine());
                dataBaseWriter.setContent(dataBaseReader.getContent());
                dataBaseWriter.setTitle(dataBaseReader.getTitle());
                dataBaseWriter.setTemplateType(dataBaseReader.getTemplateType());
                String prefix = "smspush_";
                String module = "template";
                String templateType = dataBaseReader.getTemplateType();
                int nodeId = Integer.parseInt(dataBaseReader.getNodeId());
                if (templateType.equals("邮件验证码") || templateType.equals("邮件通知") || templateType.equals("邮件营销")) {
                    prefix = "email_";
                    module = TemplateModuleEnum.getModuleNameByNodeId(nodeId).equals("") ? TemplateModuleEnum.getModuleNameByNodeId(0) : TemplateModuleEnum.getModuleNameByNodeId(nodeId);
                }
                dataBaseWriter.setLocale(dataBaseReader.getLocale());
                dataBaseWriter.setLokaliseTitleKey((prefix + module + "_" + dataBaseReader.getTemplate() + "_title").toLowerCase());
                dataBaseWriter.setLokaliseContentKey((prefix + module + "_" + dataBaseReader.getTemplate() + "_content").toLowerCase());
                writerList.add(dataBaseWriter);
            }
        }
//
        List<DataBaseReader> excel3 = readExcel3();
        List<DataBaseReader> excel4 = readExcel4();
        Map<String, List<DataBaseWriter>> writeListMap = writerList.stream().collect(Collectors.groupingBy(DataBaseWriter::getTemplate));
        for (DataBaseReader dataBaseReader : excel3) {
            if (writeListMap.containsKey(dataBaseReader.getTemplate())) {
                List<DataBaseWriter> writerList1 = writeListMap.get(dataBaseReader.getTemplate());
                DataBaseWriter dataBaseWriter = writerList1.get(0);
                dataBaseWriter.setCount(dataBaseReader.getCnt());
            }
        }
        for (DataBaseReader dataBaseReader : excel4) {
            if (writeListMap.containsKey(dataBaseReader.getTemplate())) {
                List<DataBaseWriter> writerList1 = writeListMap.get(dataBaseReader.getTemplate());
                DataBaseWriter dataBaseWriter = writerList1.get(0);
                dataBaseWriter.setCount(dataBaseReader.getCnt());
            }
        }


        String writeFileName = "C:\\Users\\hp\\Desktop\\ok\\统计完成.xlsx";
        write(writeFileName, DataBaseWriter.class).sheet().head(DataBaseWriter.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(writerList);
    }

    public static List<DataBaseReader> readExcel1(){
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\allTemplate.xlsx";
        ExcelListener<DataBaseReader> excelListener = new ExcelListener<>();
        read(fileName, DataBaseReader.class, excelListener).sheet().doRead();
        List<DataBaseReader> dataList = excelListener.getDataList();
        return dataList;
    }
    public static List<DataBaseReader> readExcel3(){
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\email_data.xlsx";
        ExcelListener<DataBaseReader> excelListener = new ExcelListener<>();
        read(fileName, DataBaseReader.class, excelListener).sheet().doRead();
        List<DataBaseReader> dataList = excelListener.getDataList();
        return dataList;
    }
    public static List<DataBaseReader> readExcel4(){
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\msg_data.xlsx";
        ExcelListener<DataBaseReader> excelListener = new ExcelListener<>();
        read(fileName, DataBaseReader.class, excelListener).sheet().doRead();
        List<DataBaseReader> dataList = excelListener.getDataList();
        return dataList;
    }

    public static List<DataBaseWriter> readExcel5(){
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\统计完成.xlsx";
        ExcelListener<DataBaseWriter> excelListener = new ExcelListener<>();
        read(fileName, DataBaseWriter.class, excelListener).sheet().doRead();
        List<DataBaseWriter> dataList = excelListener.getDataList();
        System.out.println("dataList.size():"+dataList.size());
        int count = 0;
        for (DataBaseWriter dataBaseWriter : dataList) {
            if (StringUtils.isEmpty(dataBaseWriter.getCount())) {
                count++;
                System.out.println(dataBaseWriter.getTemplate());
                System.out.println(count);
            }
//            String template = dataBaseWriter.getTemplate();
//            String content = dataBaseWriter.getContent();
//            if (StringUtils.isEmpty(content)) {
//                System.out.println(template);
//            }
        }
        return dataList;
    }

    public static void main(String[] args) {
        readExcel5();
    }

}
